$(function () {
    // 轮播图
var form = layui.form
// 获取轮播图列表数据
function loadSwiperList() {
       $.ajax({
           type: 'get',
           url: '/admin/swipers',
           success: function (res) {
               console.log(res);
               var tags = template('table-tpl', res)
               $('.layui-table tbody').html(tags)
           }
       })
   }

loadSwiperList()
// 轮播图状态切换
$('.layui-table tbody').on('click', '.layui-badge', function (e) {
      let id = $(e.target).data('id')
      let status = $(e.target).data('status')
      $.ajax({
          type: 'put',
          url: '/admin/swipers/'+id,
          data: { status },
          success: function (res) {
              if (res.status === 0) {
                // 切换成功
                layer.msg(res.message)
                loadSwiperList()
              }
          }
      })
})
  //删除功能
$('tbody').on('click', '.delete', function (e) {
    if (confirm('确定要删除吗?')) {
        let id = $(e.target).data('id')
        $.ajax({
            type:'DELETE',
            url: '/admin/swipers/' + id,
            success: function (res) {
                if (res.status === 0) {
                    layer.msg('轮播图删除')
                    loadSwiperList()
               }
            }
        })
    } else {
        return
    }
    
   
})
  //添加功能
$('#uploadSwiper').on('click', function () {
    $('#myfile').trigger('click')
  })
$('body').on('change','#myfile', function () {
    if (this.files.length > 0) {
      let fd = new FormData();
      for (let i = 0; i < this.files.length; i++) {
        // 把每个文件对象追加到fd中
        fd.append('swipers', this.files[i]);
      }
      // 循环结束，ajax提交fd即可
        $.ajax({
            type: 'POST',
            url: '/admin/swipers',
            data: fd,
            processData: false,
            contentType: false,
            success: function (res) {
                if (res.status === 0) {
                    layer.msg('文件上传成功')
                    loadSwiperList()
                }
            }
        })
    }
  })
})